
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

#include "aatracker.h"
#include "trackerlogger.h"
#include "urlhandler.h"

extern int LOG_LEVEL;

int testDecodeUrl(){

    char* testURL = "ANDR%c9%20L%20R";

    char decodedUrl[200];
    char expectedResult[9] = {0x41, 0x4e, 0x44, 0x52, 0xC9, 0x20, 0x4C, 0x20, 0x52};

    int decodedChars = decodeUrl(testURL, decodedUrl, 15);

    if(memcmp (decodedUrl, expectedResult, 9)!=0){
        puts ("1: error testing decodeUrl: decoding error. output string is not the same as input url string");
        return -1;
    }

    return OK;
}

int testIsAnnounce(){

    puts ("testing isAnnounce...");

    char requestStr[MAX_CHARS_IN_REQUEST]  = "GET /announce?peer_id=bozo HTTP/1.1\
Host: 201.35.4.149:32459\
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0_1 like Mac OS X; pt-br) \
            AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A400 Safari/528.16\
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\
Accept-Language: pt-br\
Accept-Encoding: gzip, deflate\
Connection: keep-alive";

    int res;

    res = isAnnounce(requestStr);

    if (res == FALSE){
        puts ("1: error testing isAnnounce: response marked as non-announce when it is an announce");
    }

    strcpy(requestStr, "");
    res = isAnnounce(requestStr);

    if (res == TRUE){
        puts ("1: error testing isAnnounce: response marked as announce when it is not");
    }

    strcpy(requestStr, "bozo");
    res = isAnnounce(requestStr);

    if (res == TRUE){
        puts ("2: error testing isAnnounce: response marked as announce when it is not");
    }

    res = isAnnounce(NULL);

    if (res == TRUE){
        puts ("3: error testing isAnnounce: response marked as announce when it is not");
    }

    puts ("isAnnounce test finished");

    return OK;

};

int testIsScrape(){

    puts ("testing isScrape...");

    char requestStr[MAX_CHARS_IN_REQUEST]  = "GET /scrape?peer_id=bozo HTTP/1.1\
Host: 201.35.4.149:32459\
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0_1 like Mac OS X; pt-br) \
            AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A400 Safari/528.16\
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\
Accept-Language: pt-br\
Accept-Encoding: gzip, deflate\
Connection: keep-alive";

    int res;

    res = isScrape(requestStr);

    if (res == FALSE){
        puts ("1: error testing isScrape: response marked as non-scrape when it is an scrape");
    }

    strcpy(requestStr, "");
        res = isScrape(requestStr);

    if (res == TRUE){
        puts ("1: error testing isScrape: response marked as scrape when it is not");
    }

    strcpy(requestStr, "bozo");
    res = isScrape(requestStr);

    if (res == TRUE){
        puts ("2: error testing isScrape: response marked as scrape when it is not");
    }

    res = isScrape(NULL);

    if (res == TRUE){
        puts ("3: error testing isScrape: response marked as scrape when it is not");
    }

    puts ("isScrape test finished");

    return OK;

}

int testGetAnnounceParameters(){

    puts ("testing getAnnounceParameters...");

    char requestStr[MAX_CHARS_IN_REQUEST] ="GET /announce?peer_id=12345678901234567890&info_hash=abcdeabcdeabcdeabcde\
&event=started&compact=1&downloaded=5&uploaded=5&left=5&numwant=30 \
HTTP/1.1\
Host: 201.35.4.149:32459\
User-Agent: Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0_1 like Mac OS X; pt-br) \
AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A400 Safari/528.16\
Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\
Accept-Language: pt-br\
Accept-Encoding: gzip, deflate\
Connection: keep-alive";

    AnnounceParameters ap;
    ap.info_hash[0] ='\0';

    getAnnounceParameters(requestStr, &ap);

    if (ap.compact!=1){
        puts("error testing getAnnounceParameters: compact is not the correct value");
    }

    if (ap.downloaded!=5){
            puts("error testing getAnnounceParameters: downloaded is not the correct value");
    }
    if (ap.uploaded!=5){
                puts("error testing getAnnounceParameters: uploaded is not the correct value");
        }
    if (ap.left!=5){
                puts("error testing getAnnounceParameters: left is not the correct value");
        }
    if (ap.numwant!=30){
                puts("error testing getAnnounceParameters: numwant is not the correct value");
        }
    if (ap.event!=STARTED){
                puts("error testing getAnnounceParameters: event is not the correct value");
        }
    /*if (ap.peer_id!=){
                puts("error testing getAnnounceParameters: compact is not the correct value");
        }
    if (ap.info_hash!=){
                puts("error testing getAnnounceParameters: compact is not the correct value");
        }
    if (ap.port!=){
                puts("error testing getAnnounceParameters: compact is not the correct value");
        }
        */

    puts ("getAnnounceParameters test finished");
    return OK;


}

int runUrlHandlerTestSuite(){

    printf("Iniciando Suite de Testes de UrlHandler \n");

    LOG_LEVEL = CRITICAL;

    testDecodeUrl();

    testIsAnnounce();

    testIsScrape();

    testGetAnnounceParameters();

    printf("Suite de Testes de urlHandler finalizada\n");

    return OK;

}


/*int testDecodeUrl(char stringsToTest[][300], int *expectedResults, int numberOfTests){

    printf("Iniciando testDecodeUrl \n");

    int i=0;
    int resultOfTest=0, successfullTests=0, failedTests=0;

    struct timeval timeOfStartOfTests, timeOfEndOfTests;

//    gettimeofday(&timeOfStartOfTests, NULL);



    for (i = 0; i < numberOfTests; ++i) {


        char decodedUrl[20];
        gettimeofday(&timeOfStartOfTests, NULL);

        resultOfTest= decodeUrl(stringsToTest[i], decodedUrl, strlen(stringsToTest[i]));

        gettimeofday(&timeOfEndOfTests, NULL);

        if (resultOfTest == expectedResults[i]){
            successfullTests++;
        }
        else {failedTests++; printf ("%d\n", resultOfTest);}

    }

  //  gettimeofday(&timeOfEndOfTests, NULL);

    printf("Testes de testDecodeUrlParameters finalizados\n");
    printf("Tempo Gasto com Testes=%lf \n", (double)timeOfEndOfTests.tv_usec - timeOfStartOfTests.tv_usec);

    printf("Testes executados com sucesso=%d \n",successfullTests);
    printf("Testes que falharam=%d \n",failedTests);


    return 0;

}
*/


/*char stringsToTest[20][300] ={"%BC4Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A"    };


int expectedResults[20] = {
        20};

testDecodeUrl(stringsToTest, expectedResults, 1);

*/
